package com.zwr.sheller.shop.service.impl;

import com.zwr.sheller.core.common.WQQBusinessException;
import com.zwr.sheller.core.common.WQQExceptionCode;
import com.zwr.sheller.core.pojo.Store;
import com.zwr.sheller.core.util.GetThreadLocal;
import com.zwr.sheller.core.util.OSSUtil;
import com.zwr.sheller.core.util.ThreadLocalUtil;
import com.zwr.sheller.shop.mapper.StoreMapper;
import com.zwr.sheller.shop.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 18032
 * @Date: 16:26 2022/1/5
 */
@Service
public class StoreServiceImpl implements StoreService {
    @Autowired(required = false)
    StoreMapper storeMapper;



    @Override
    public Store selectByStoreId(Long storeId) throws WQQBusinessException {
        Store store = storeMapper.selectByPrimaryKey(storeId);
        if (store == null){
            throw new WQQBusinessException(WQQExceptionCode.UNKNOWN_EXCEPTION);
        }
        return store;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Integer updateStoreBaseMessage(Store store) throws WQQBusinessException {
        Map<String,Object> map = new HashMap<>(1);
        map.put("storeId",1L);
        map.put("storeIID","213");
        ThreadLocalUtil.setObject(map);

        //店铺信息进行修改
        Long storeId = GetThreadLocal.getStoreId();
        store.setStoreId(storeId);

        if (storeId==0L){
            throw new WQQBusinessException(WQQExceptionCode.UNKNOWN_EXCEPTION);
        }
        //图片地址修改
        System.out.println(store.getStoreLogo()+"我偶是logo");
        if (!StringUtils.isEmpty(store.getStoreLogo())){
            String fileName = store.getStoreLogo().substring(37);
            OSSUtil.copyFile("temporary/"+fileName,"storePicture"+fileName);
            OSSUtil.delete("temporary/"+fileName);
            store.setStoreLogo("http://47.108.60.93/ossImg/storePicture"+fileName);
        }

        //修改的才改,为空的不改
        Integer i = storeMapper.updateByPrimaryKeySelective(store);

        return i;
    }

    @Override
    public Map<String, Object> userGetStoreMessage(Long storeId) throws WQQBusinessException {
        Store store = storeMapper.selectByPrimaryKey(storeId);
        if (store == null){
            throw  new WQQBusinessException(WQQExceptionCode.FAILED_SELECT_STORE);
        }
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("storeName",store.getStoreName());
        map.put("storeLogo",store.getStoreLogo());
        return map;
    }
}
